package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyDiscussQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/strategys")
public class StrategyController {
    @Autowired
    private IStrategyService service;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private IStrategyDetailService detailService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IStrategyDiscussService discussService;
    @Autowired
    private IStrategyLikeService likeService;

    /**
     * 查询推荐的大攻略
     * 动作:GET
     * 资源:/strategys/commends
     * 参数:state=2
     *
     * @return
     */
    @GetMapping("/commends")
    public Object listCommend() {
        List<Strategy> list = service.listCommend();
        return list;
    }

    /**
     * 根据大攻略id查询攻略分类
     * 动作:GET
     * 资源:/strategies/{id}/catalogs
     * 参数:
     *
     * @return
     */
    @GetMapping("/{id}/catalogs")
    public Object getCatalog(@PathVariable Long id) {
        List<StrategyCatalog> catalogs = catalogService.selectByStrategyId(id);
        return catalogs;
    }

    /**
     * 根据id查询攻略文章
     * 动作:GET
     * 资源:/strategies/details/{id}
     * 参数:
     *
     * @return
     */
    @GetMapping("/details/{id}")
    public Object getStrategyContent(@PathVariable Long id) {
        StrategyDetail detail = detailService.selectById(id);
        return detail;

    }


    //回显攻略点评
    @GetMapping("/comment/{strategyId}")
    public Object getReleaseList(@ModelAttribute("qo") StrategyCommentQueryObject qo, Model model) {
        qo.setOrderBy("createTime desc");
        PageInfo<StrategyComment> result = commentService.selectList(qo);
        model.addAttribute("result", result);
        return result;
    }


    @GetMapping("/strategyComment/{id}")
    public Object getComment(Long id) {
        StrategyComment strategyComment = commentService.selectById(id);

        return strategyComment;
    }


    //新增攻略点评
    @PostMapping("/comment")
    public Object insert(StrategyComment comment, MultipartFile[] imgs) {
        JSONResult result = new JSONResult();
        List<StrategyCover> images = comment.getImages();
        for (int i = 0; i < imgs.length; i++) {
            //得到图片名
            MultipartFile img = imgs[i];
            System.out.println(img);
            if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
                String url = QiniuCloudUtil.uploadQiniu(img);
                if (i == 0) {
                    comment.setCoverUrl(url);
                }
                images.add(new StrategyCover(null,url));
            }
        }
        try {
            commentService.saveOrUpdate(comment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }

    //评论回显系统
    @GetMapping("/strategyDiscuss/{commentId}")
    public Object getComment(StrategyDiscussQueryObject qo) {
        qo.setOrderBy("createTime desc");
        PageInfo<StrategyDiscuss> result = discussService.selectList(qo);
        return result;
    }

    //评论数量回显系统
    @GetMapping("/strategyDiscuss/count/{commentId}")
    public Object getCount(Long commentId) {
        return discussService.selectCount(commentId);

    }


    //新增攻略评论评论
    @PostMapping("/strategyDiscuss")
    public Object insert(StrategyDiscuss discuss) {
        JSONResult result = new JSONResult();

        try {
            discussService.saveOrUpdate(discuss);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }
    /*查询点赞*/
    @GetMapping("/strategyLike/{strategyId}")
    public Object getLike(Long userId,Long strategyId) {
        return likeService.selectLike(userId, strategyId);
    }

   //保存点赞数据
    @PostMapping("/strategyLike")
    public Object addLike(StrategyLike like) {
        JSONResult result = new JSONResult();

        try {
            likeService.saveOrUpdate(like);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //删除点赞数据
    @DeleteMapping("/strategyLike/{id}")
    public Object deleteLike(Long userId,Long strategyId) {
        JSONResult result = new JSONResult();

        try {
            likeService.delete(userId,strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


}
